<?php

include ("../lib/BLL/DonViBLL.php");
include ("../lib/BLL/DmNganhBLL.php");
include ("../lib/BLL/DonViHocVuBLL.php");
include ("../lib/BLL/DmMonHocBLL.php");
include ("../lib/BLL/DmCaHocBLL.php");
include ("../lib/BLL/KhoaHocBLL.php");
include ("../lib/BLL/LopMonBLL.php");
include ("../lib/BLL/CanBoBLL.php");
include ("../lib/BLL/LapLichBLL.php");
include ("../lib/BLL/ChiTietLopMonBLL.php");
include ("../lib/BLL/QuaTrinhLopMonBLL.php");

$donvi=new DonViBLL();
$nganh=new DmNganhBLL();
$dvhv=new DonViHocVuBLL();
$monhoc= new DmMonHocBLL();
$cahoc= new DmCaHocBLL();
$khoahoc= new KhoaHocBLL();
$canbo= new CanBoBLL();
$lopmonBLL=new LopMonBLL();
$laplich=new LapLichBLL();
$ctlm= new ChiTietLopMonBLL();
$qllm= new QuaTrinhLopMonBLL();


$lopmonDAL=new LopMon();
$laplichDAL=new LapLich();
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$lich=$nganh_lopmon=$ds_lichthi=$dscb_lopmon=null;
$khoa="";
$ma_cb=$_SESSION['ma_cb'];

if(isset($_POST['sua'])){
    $dong=$_POST['dongsua'];
    $ma_lopmon=$_POST['lopmon'.$dong];
    $ngay1=$_POST["ngay1".$dong];
    $ngay2=$_POST["ngay2".$dong];
    $thi1=$_POST["ngaythi1".$dong];
    $thi2=$_POST["ngaythi2".$dong];
    $ca1=$_POST["cathi1".$dong];
    $ca2=$_POST["cathi2".$dong];
    $sql_update_lopmon="UPDATE tbl_lopmon SET ngaybatdau='$ngay1',ngayketthuc='$ngay2' WHERE ma_lopmon='$ma_lopmon'";
    mysql_query($sql_update_lopmon);
    $mact_lm=$ma_lopmon."hoc1";
    $sql_update_cthoc1="UPDATE tbl_chitiet_lopmon SET ngayhoc_ctlm='$ngay1' WHERE ma_chitiet_lopmon='$mact_lm'";
    mysql_query($sql_update_cthoc1);
    
    $mact_lm=$ma_lopmon."hoc2";
    $sql_update_cthoc2="UPDATE tbl_chitiet_lopmon SET ngayhoc_ctlm='$ngay2' WHERE ma_chitiet_lopmon='$mact_lm'";
    mysql_query($sql_update_cthoc2);
    
    $mact_lm=$ma_lopmon."thi1";
    $sql_update_ctthi1="UPDATE tbl_chitiet_lopmon SET madm_ca='$ca1',ngayhoc_ctlm='$thi1' WHERE ma_chitiet_lopmon='$mact_lm'";
    mysql_query($sql_update_ctthi1);
    
    $mact_lm=$ma_lopmon."thi2";
    $sql_update_ctthi2="UPDATE tbl_chitiet_lopmon SET madm_ca='$ca2',ngayhoc_ctlm='$thi2' WHERE ma_chitiet_lopmon='$mact_lm'";
    mysql_query($sql_update_ctthi2);
}


if(isset($_POST['laplich'])){
    if(isset($_POST['checklopmon'])){
        $arr_check = $_POST['checklopmon'];
        $nhom=date('HisdmY');
        $ma_cb=$_SESSION['ma_cb'];
        $dslopmon="";
        foreach($arr_check as $key => $malopmon){
            $dslopmon.="'".$malopmon."',";    
        }
        $dslopmon=rtrim($dslopmon,',');
        $sql_khoa="   SELECT    DISTINCT tbl_lop.ma_lop,ten_lop 
                      FROM      tbl_quatrinhlopmon, tbl_quanlylop, tbl_lop
                      WHERE     madm_ttlm =  'lophc'
                      AND       tbl_quatrinhlopmon.noidungthuchien = tbl_quanlylop.ma_lop
                      AND       tbl_lop.ma_lop = tbl_quanlylop.ma_lop
                      AND	    ma_lopmon IN ($dslopmon)";
                      
        $ex_khoa=mysql_query($sql_khoa);
        if($ex_khoa){
            while($row=mysql_fetch_array($ex_khoa)){
                $ex=mysql_query("SELECT ma_lop FROM `tbl_quanlylop` WHERE ma_lop='".$row['ma_lop']."' AND ma_cb='$ma_cb'");
                    if(mysql_num_rows($ex)>0){
                        $khoa.=$row['ten_lop']." - ";
                    }
            }
        }
        $khoa=rtrim($khoa,' - ');                              
        
        foreach($arr_check as $key => $malopmon){
            $laplichDAL->setLapLich($malopmon,$ma_cb,null,$nhom,$khoa);
            $laplich->InsertLaplichBLL($laplichDAL);
        }
        
    }
}


if(isset($_POST['xem'])){
    $madonvi=$_POST['donvi'];
    $madvhv=$_POST['dvhv'];
    $makhoahoc=$_POST['khoahoc'];
    $lich=$lopmonBLL->getLich_Donvi_HocVu_CB($madonvi,$madvhv,$ma_cb);
    if(count($lich)!=0){
        $nganh_lopmon=$lopmonBLL->getnganh_lopmon($madonvi,$madvhv);
        $dslopmon="";
        foreach($lich as $lm){
            $dslopmon.="'".$lm['ma_lopmon']."',";
        }
        $dslopmon=rtrim($dslopmon,',');
        $ds_lichthi=$ctlm->Getngaythi_lopmon($dslopmon);
        $dscb_lopmon=$qllm->GetCB_lopmon($dslopmon);              
    }
}else{
    $madonvi='';//$_POST['donvi'];
    $madvhv='1-2013';//$_POST['dvhv'];
    $makhoahoc='2013';//$_POST['khoahoc'];
    $lich=$lopmonBLL->getLich_Donvi_HocVu_CB($madonvi,$madvhv,$ma_cb);
    if(count($lich)!=0){
        $nganh_lopmon=$lopmonBLL->getnganh_lopmon($madonvi,$madvhv);
        $dslopmon="";
        foreach($lich as $lm){
            $dslopmon.="'".$lm['ma_lopmon']."',";
        }
        $dslopmon=rtrim($dslopmon,',');
        $ds_lichthi=$ctlm->Getngaythi_lopmon($dslopmon);
        $dscb_lopmon=$qllm->GetCB_lopmon($dslopmon);
    }
}

$smarty->assign('lich',$lich);
$smarty->assign('ds_lichthi',$ds_lichthi);
$smarty->assign('nganh_lopmon',$nganh_lopmon);
$smarty->assign('soluonglopmon',count($lich));
$smarty->assign('dscb_lopmon',$dscb_lopmon);
$smarty->assign('ma_cb',$_SESSION['ma_cb']);
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

$ds_donvi=$donvi->getListDonVi_Lienket();
$smarty->assign('ds_donvi',$ds_donvi);

$ds_dvhv= $dvhv->getListDonViHocVu();
$smarty->assign('ds_dvhv',$ds_dvhv);

$ds_cahoc=$cahoc->getListDmCaHoc();
$smarty->assign('ds_cahoc',$ds_cahoc);

$ds_namkhoahoc=$khoahoc->getListNamKhoaHoc();
$smarty->assign('ds_namkhoahoc',$ds_namkhoahoc);

$ds_canbo=$canbo->getListCanBo();
$smarty->assign('ds_canbo',$ds_canbo);


$smarty->display('daotao/xemkehoach.tpl');   
?>